/*
 * Copyright 2004-2006 公司名
 * 本软件及其所有拷贝的名称，与光盘上或本软件中注明的公司同在。
 * 本软件及文档享有版权，并受国家版权法及国际协约条款的保护。
 * 您不可以从本软件中去掉其版权声明；并保证为本软件的拷贝（本部或部分）复制版权声明。
 * 您同意制止以任何形式非法拷贝本软件及文档。
 * 版权信息参见：
 *      http://www.companyname.cn/licenses
 */

package com.companyname.common.sysframe.valid;

import java.util.LinkedHashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.companyname.common.sysframe.model.Unit;
import com.companyname.common.util.mvc.valid.UserValidator;
import com.companyname.common.util.mvc.valid.ValidatedResult;
import com.companyname.common.viewframe.define.ColumnType;
import com.companyname.common.viewframe.manager.ColumnManager;
import com.companyname.common.viewframe.model.Area;
import com.companyname.common.viewframe.model.Column;
import com.companyname.common.viewframe.util.CompanyNameViewFrameException;

/**
 * <p>Title: 添加视图2系统管理实体</p>
 * <p>Description:  </p>
 * <p>Copyright: Copyright (c) 2004-2006</p>
 * <p>Company: 公司名</p>
 * @ $Author: 作者名 $
 * @ $Date: 创建日期 $
 * @ $Revision: 1.0 $
 * @ created in 创建日期
 *
 */

public abstract  class AddViews2SysUnitValidator extends UserValidator
{
        static Logger logger = Logger.getLogger(AddViews2SysUnitValidator.class);

        // 取得主页
        public Set getHomes(Set areas)
        {
                logger.debug("取得homes");
                Set homes =  new LinkedHashSet();
                Object[] as = areas.toArray();
                for(int i = 0; i < as.length; i ++)
                {
                        Area area = (Area)as[i];
                        homes.add(area.getHome());
                }
                return homes;
        }
        
        // 取得分区
        public Set getAreas(Set column1s)
        {
                logger.debug("取得areas");
                Set areas =  new LinkedHashSet();
                Object[] c1s = column1s.toArray();
                for(int i = 0; i < c1s.length; i ++)
                {
                        Column c1 = (Column)c1s[i];
                        areas.add(c1.getArea());
                }
                return areas;
        }
        
        //取得一级栏目
        public Set getColumn1s(Set column2s)
        {
                Set c1s = new LinkedHashSet();
                Object[] c2s = column2s.toArray();
                for(int i = 0; i < c2s.length; i ++)
                {
                        Column c2 = (Column)c2s[i];
                        c1s.add(c2.getSuperColumn());
                }
                return c1s;
        }
        
        // 取得二级栏目
        public Set getColumn2s(HttpServletRequest request, ValidatedResult vr)
        {
                logger.debug("取得columns");
                Set c2s =  new LinkedHashSet();
                ColumnManager columnManager = (ColumnManager)this.getBean("columnManager");
                String[] ids = request.getParameterValues("columnId");
                if(ids != null)
                {
                        for(int i = 0; i < ids.length; i ++)
                        {
                                try
                                {
                                        Column c2 = (Column)columnManager.get(ids[i]);
                                        if(c2.getType()== ColumnType.TWO_LEVEL)
                                        {
                                                c2s.add(c2);
                                        }
                                        else
                                        {
                                                throw new CompanyNameViewFrameException(c2.getName() + "不是二级目录");
                                        }
                                }
                                catch(Exception x)
                                {
                                        logger.debug("id为" + ids[i] + "的栏目已经被删除");
                                }
                        }
                }
                return c2s;
        }
        
        // 取得系统管理实体
        public abstract Unit getUnit(HttpServletRequest request, ValidatedResult vr);

        @Override
        public void validing(HttpServletRequest request, ValidatedResult vr, String[] params)
        {
                logger.debug("开始校验待保存的系统管理实体");
                Set column2s = this.getColumn2s(request,vr);
                Set column1s = this.getColumn1s(column2s);
                Set columns = new LinkedHashSet();
                columns.addAll(column1s);
                columns.addAll(column2s);
                Set areas = this.getAreas(column1s);
                Set homes = this.getHomes(areas);
                Unit unit = this.getUnit(request,vr);
                unit.setHomes(homes);
                unit.setAreas(areas);
                unit.setColumns(columns);
                request.setAttribute(unit.getClass().getName(),unit);
                
        }
}
